const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'chatime',
  port: 3306
};

async function testAvatarSystem() {
  const pool = mysql.createPool(dbConfig);
  
  try {
    console.log('🔗 连接数据库...');
    await pool.getConnection();
    console.log('✅ 数据库连接成功');
    
    // 测试1: 检查所有用户是否有头像
    console.log('\n📋 测试1: 检查用户头像分配');
    const [users] = await pool.execute(`
      SELECT id, name, avatar FROM users ORDER BY name
    `);
    
    console.log(`📊 找到 ${users.length} 个用户`);
    let allHaveAvatars = true;
    users.forEach(user => {
      if (!user.avatar) {
        allHaveAvatars = false;
        console.log(`❌ 用户 ${user.name} 没有头像`);
      } else {
        console.log(`✅ 用户 ${user.name}: ${user.avatar}`);
      }
    });
    
    if (allHaveAvatars) {
      console.log('✅ 所有用户都有头像');
    } else {
      console.log('❌ 存在用户没有头像');
    }
    
    // 测试2: 检查头像格式是否正确
    console.log('\n📋 测试2: 检查头像格式');
    const validAvatarPattern = /^avatar[1-9]|avatar1[0-2]$/;
    let allValidFormat = true;
    
    users.forEach(user => {
      if (user.avatar && !validAvatarPattern.test(user.avatar)) {
        allValidFormat = false;
        console.log(`❌ 用户 ${user.name} 的头像格式不正确: ${user.avatar}`);
      }
    });
    
    if (allValidFormat) {
      console.log('✅ 所有头像格式正确');
    } else {
      console.log('❌ 存在头像格式不正确');
    }
    
    // 测试3: 测试头像更新功能
    console.log('\n📋 测试3: 测试头像更新功能');
    if (users.length > 0) {
      const testUser = users[0];
      const originalAvatar = testUser.avatar;
      const newAvatar = originalAvatar === 'avatar1' ? 'avatar2' : 'avatar1';
      
      console.log(`🔄 测试用户: ${testUser.name}`);
      console.log(`📸 原头像: ${originalAvatar}`);
      console.log(`📸 新头像: ${newAvatar}`);
      
      // 更新头像
      await pool.execute(`
        UPDATE users SET avatar = ? WHERE id = ?
      `, [newAvatar, testUser.id]);
      
      // 验证更新
      const [updatedUser] = await pool.execute(`
        SELECT avatar FROM users WHERE id = ?
      `, [testUser.id]);
      
      if (updatedUser[0].avatar === newAvatar) {
        console.log('✅ 头像更新成功');
      } else {
        console.log('❌ 头像更新失败');
      }
      
      // 恢复原头像
      await pool.execute(`
        UPDATE users SET avatar = ? WHERE id = ?
      `, [originalAvatar, testUser.id]);
      console.log('🔄 已恢复原头像');
    }
    
    // 测试4: 统计头像使用情况
    console.log('\n📋 测试4: 头像使用统计');
    const avatarStats = {};
    users.forEach(user => {
      const avatar = user.avatar || '无头像';
      avatarStats[avatar] = (avatarStats[avatar] || 0) + 1;
    });
    
    console.log('📈 头像使用情况：');
    Object.entries(avatarStats).forEach(([avatar, count]) => {
      console.log(`  ${avatar}: ${count} 个用户`);
    });
    
    // 检查是否有重复使用
    const duplicates = Object.entries(avatarStats).filter(([avatar, count]) => count > 1);
    if (duplicates.length > 0) {
      console.log('⚠️  存在头像重复使用：');
      duplicates.forEach(([avatar, count]) => {
        console.log(`  ${avatar}: ${count} 个用户`);
      });
    } else {
      console.log('✅ 没有头像重复使用');
    }
    
    console.log('\n🎉 头像系统测试完成！');
    
  } catch (error) {
    console.error('❌ 测试失败:', error);
  } finally {
    await pool.end();
  }
}

// 运行测试
testAvatarSystem(); 